package com.sls.nzdapplication.ui.custom;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;


import com.sls.nzdapplication.model.WellSectionHole;

import java.sql.Time;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

@SuppressLint("AppCompatCustomView")
public class MoveView extends ImageView {
    private int x;

    private int y;

    long timeDown, timeMove;
    //是否是长按事件
    boolean isLongClick;
    //移动相关
    float downX, downY, moveX, moveY;
    boolean isLongClickModule = false;

    private WellSectionHole wellSectionHole;

    private OnMoveClickListener onMoveClickListener;

    private boolean isclickModule = false;

    public MoveView(Context context) {
        this(context, null);
    }

    public MoveView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MoveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnMoveClickListener(OnMoveClickListener onMoveClickListener){
        this.onMoveClickListener = onMoveClickListener;
    }

    public void setWellSectionHole(WellSectionHole wellSectionHole){
        this.wellSectionHole = wellSectionHole;
    }

    static boolean isLongPressed(float lastX, float lastY, float thisX,
                                 float thisY, long lastDownTime, long thisEventTime,
                                 long longPressTime) {
        float offsetX = Math.abs(thisX - lastX);
        float offsetY = Math.abs(thisY - lastY);
        long intervalTime = thisEventTime - lastDownTime;
        if (offsetX <= 10 && offsetY <= 10 && intervalTime >= longPressTime) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int) event.getX();
        y = (int) event.getY();


        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                timeDown = System.currentTimeMillis();
                isLongClick = false;
                downX = event.getRawX();
                downY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:

                timeMove = System.currentTimeMillis();
                long durationMs = timeMove - timeDown;
                //Log.d(TAG, "onTouch: durationMs="+durationMs);
                if (durationMs > 500) {
                    isLongClick = true;
                    //长按事件，可以移动
                    moveX = event.getRawX();
                    moveY = event.getRawY();
                    //移动的距离
                    float dx = moveX - downX;
                    float dy = moveY - downY;
                    //重新设置控件的位置。移动
                    setX(x + getLeft() + getTranslationX() - getWidth() / 2);
                    setY(y + getTop() + getTranslationY() - getHeight() / 2);
                    //重置
                    downX = moveX;
                    downY = moveY;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (!isLongClick)
                   onMoveClickListener.OnMoveViewClickListener(wellSectionHole);
                break;
        }
        return true;
    }

    public interface OnMoveClickListener{
        void OnMoveViewClickListener(WellSectionHole wellSectionHole);
        void OnMoveViewLongClickListener(WellSectionHole wellSectionHole);
    }
}
